package com.sgcc.isc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.sgcc.isc.config.Global;
import com.sgcc.isc.websocket.service.system.SystemInfoSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.isc.domain.SysDistrict;
import com.sgcc.isc.service.ISysDistrictService;

/**
 * 部门映射Controller
 * 
 * @author sgcc
 * @date 2021-03-17
 */
@RestController
@RequestMapping("/district")
public class SysDistrictController extends BaseController
{
    @Autowired
    private ISysDistrictService sysDistrictService;

    /**
     * 查询部门映射列表
     */
//    //@PreAuthorize(hasPermi="isc:district:list")
    @GetMapping("/list")
    public AjaxResult list(SysDistrict sysDistrict)
    {
        List<SysDistrict> list = sysDistrictService.selectSysDistrictList(sysDistrict);
        return AjaxResult.success(list);
    }

    /**
     * 导出部门映射列表
     * @throws IOException 
     */
//    //@PreAuthorize(hasPermi="isc:district:export")
    @Log(title = "部门映射", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response,SysDistrict sysDistrict) throws IOException
    {
        List<SysDistrict> list = sysDistrictService.selectSysDistrictList(sysDistrict);
        ExcelUtil<SysDistrict> util = new ExcelUtil<SysDistrict>(SysDistrict.class);
        util.exportExcel(response,list, "district");
    }

    /**
     * 获取部门映射详细信息
     */
//    //@PreAuthorize(hasPermi="isc:district:query")
    @GetMapping(value = "/{districtId}")
    public AjaxResult getInfo(@PathVariable("districtId") String districtId)
    {
        return AjaxResult.success(sysDistrictService.selectSysDistrictById(districtId));
    }

    /**
     * 新增部门映射
     */
//    //@PreAuthorize(hasPermi="isc:district:add")
    @Log(title = "部门映射", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDistrict sysDistrict)
    {
        return toAjax(sysDistrictService.insertSysDistrict(sysDistrict));
    }

    /**
     * 修改部门映射
     */
//    //@PreAuthorize(hasPermi="isc:district:edit")
    @Log(title = "部门映射", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDistrict sysDistrict)
    {
        SysDistrict data=sysDistrictService.selectSysDistrictById(sysDistrict.getDistrictId());
        if(null==data){
            return toAjax(sysDistrictService.insertSysDistrict(sysDistrict));
        }else{
            return toAjax(sysDistrictService.updateSysDistrict(sysDistrict));
        }

    }

    /**
     * 删除部门映射
     */
//    //@PreAuthorize(hasPermi="isc:district:remove")
    @Log(title = "部门映射", businessType = BusinessType.DELETE)
	@DeleteMapping("/{districtIds}")
    public AjaxResult remove(@PathVariable String[] districtIds)
    {
        return toAjax(sysDistrictService.deleteSysDistrictByIds(districtIds));
    }

    @GetMapping("/send")
    public AjaxResult get(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type",1);
        SystemInfoSocketHandler.sendMessageToAllUsers(jsonObject.toJSONString());
        return AjaxResult.success();
    }
}
